<?php
    /**
     * ApiHandleException.php
     *
     * Created by PhpStorm.
     * author: liuml  <liumenglei0211@163.com>
     * DateTime: 2018/7/27  16:37
     */

    namespace app\wxapi;

    use think\exception\Handle;
    use think\exception\HttpException;
    use think\Log;

    class ApiHandleException extends Handle
    {
        public $httpCode = 200;

        public function render(\Exception $e)
        {
            if ($e instanceof HttpException) {
                $statusCode = $e->getStatusCode();
            }

            if (!isset($statusCode)) {
                $statusCode = 500;
            }
            $this->message = $this->returnMessage($statusCode);
            $result        = [
                'code'      => $statusCode,
                'message'   => $this->message,
                'exception' => $e->getMessage(),
            ];

            Log::write('执行出错，状态码：' . $e->getCode() . '报错信息：' . $e->getMessage(), 'wxapi');
            dingding('天择旅行api出错了', $e->getCode(), $e->getMessage(), request()->url(), input('param.'), $e->getFile(), $e->getLine());
            // echo '执行出错，状态码：' . $e->getStatusCode().'报错信息：'.$e->getMessage();die;
            return json($result, $this->httpCode);
        }

        /**
         * 返回状态码中文提示
         * @param $status
         * @return string
         */
        public function returnMessage($status)
        {
            $message = [
                '401' => '签名校验错误',
                '403' => '非法访问',
                '404' => '请求路径不存在',
                '500' => '服务器内部错误',
            ];
            return $message[$status] ? : 'error';
        }

    }

